﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using LumenWorks.Framework.IO.Csv;
using System.IO;
using System.Globalization;
using ebay.Dal.CenterDB_BasTableAdapters;
using System.Data.OleDb;
using System.Collections;
using System.Data.SqlClient;


/// <summary>
///SyncExcel 的摘要说明
/// </summary>
public class SyncExcel
{
    public SyncExcel()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    decimal GetResule(string val)
    {
        val = val.Replace(",", "");
        decimal result = 0m;
        if (decimal.TryParse(val, out result))
        {
            return result;
        }
        return 0m;
    }

    string GetString(Dictionary<string, string> keyValue, string key, CsvReader csv)
    {   
        return keyValue[key] == "" ? "" : csv[keyValue[key]];
    }
    
    DateTime GetDateTime(string text, string formatter)
    {
        return text == "" ? new DateTime(1900, 1, 1) : DateTime.ParseExact(text, formatter, CultureInfo.InvariantCulture);
    }

    public bool WriteToServer(string fileName, ref int Count)
    {
        ebay.Dal.PPOrder p = new ebay.Dal.PPOrder();

        using (CsvReader csv = new CsvReader(new StreamReader(fileName), true))
        {
            using (CsvReader csvclone = new CsvReader(new StreamReader(fileName), true))
            {
                return p.WriteToServer(csv, csvclone, ref Count);
            }
        }
    }

    public bool WriteToK3XOUT(string fileName, ref int Count)
    {
        ebay.Dal.PPOrder p = new ebay.Dal.PPOrder();

        using (CsvReader csv = new CsvReader(new StreamReader(fileName), true))
        {
            using (CsvReader csvclone = new CsvReader(new StreamReader(fileName), true))
            {
                return p.WriteToK3XOUT(csv, csvclone, ref Count);
            }
        }
    }

    public List<ebay.Model.EbaySelling> GetEbaySelling(string fileName ,string ebayUserId)
    {        
        List<ebay.Model.EbaySelling> list = new List<ebay.Model.EbaySelling>();           
             
        ArrayList al = ExcelHelper.ExcelSheetName(fileName);
        DateTime importDate = DateTime.Now; 
        foreach (string sheetnames in al)
        {
            string name = sheetnames;
            DataTable dt = ExcelHelper.ExcelData(fileName, name);
            if (dt == null || dt.Rows.Count == 0)
            {
                break;
            }
            foreach (DataRow r in dt.Rows)
            {
                ebay.Model.EbaySelling m = new ebay.Model.EbaySelling();
                m.EbayUserId = r["EbayUserId"].ToString();
                m.BuyerAddress1 =r["BuyerAddress1"].ToString ();
                m.BuyerAddress2 = r["BuyerAddress2"].ToString();
                m.BuyerCity = r["BuyerCity"].ToString();
                try
                {
                    m.BuyerCounty = r["BuyerCounty"].ToString();
                    m.BuyerPhone = r["BuyerPhoneNumber"].ToString();
                }
                catch
                {
                    m.BuyerCounty = "";                   
                    m.BuyerPhone = "";
                }
                m.BuyerCountry = r["BuyerCountry"].ToString();
                m.BuyerEmail = r["BuyerEmail"].ToString();
                m.BuyerFullName = r["BuyerFullName"].ToString();
                m.BuyerPostcode = r["BuyerPostcode"].ToString();
                m.BuyerSelectedShipping = r["BuyerSelectedShipping"].ToString();
                m.BuyerState = r["BuyerState"].ToString();              
                m.CheckoutDate = Convert.ToDateTime(r["CheckoutDate"].ToString());

                m.CheckoutStatus = r["CheckoutStatus"].ToString();
                m.CompleteStatus = r["CompleteStatus"].ToString();
                m.CustomLable = r["CustomLable"].ToString();
                m.DispatchDate = Convert.ToDateTime(r["DispatchDate"].ToString());
                m.EBayPaymentStatus = r["EBayPaymentStatus"].ToString();                
                m.ExternalTransactionTime = Convert.ToDateTime(r["ExternalTransactionTime"].ToString());

                m.FeedbackLeft = r["FeedbackLeft"].ToString();
                m.FeedbackReceived = r["FeedbackReceived"].ToString();
                m.FeedbackScore = Convert.ToInt32(r["FeedbackScore"].ToString());
                m.FeeOrCreditAmount = Convert.ToDouble(r["FeeOrCreditAmount"].ToString());
                m.FinalValueFee = Convert.ToDouble(r["FinalValueFee"].ToString());
                m.ImportDate = importDate;

                m.IncludedVATRate = r["IncludedVATRate"].ToString();
                m.Insurance = Convert.ToDouble(r["Insurance"].ToString());
                m.InvoiceDate = Convert.ToDateTime(r["InvoiceDate"].ToString());
                m.InvoiceNumber = r["InvoiceNumber"].ToString();
                m.ItemId = r["ItemId"].ToString();
                m.ItemTitle = r["ItemTitle"].ToString();
                m.LastTimeModified = Convert.ToDateTime(r["LastTimeModified"].ToString());
                m.MatchDate = new DateTime(1900, 1, 1);

                m.NotestoYourself = r["NotestoYourself"].ToString();
                m.OrderId = r["OrderId"].ToString();
                m.OrderLineItemId = r["OrderLineItemId"].ToString();
                m.OrderSalesRecordNumber = r["OrderSalesRecordNumber"].ToString();
                m.PaidonDate = Convert.ToDateTime(r["PaidonDate"].ToString());

                m.PaymentMethod = r["PaymentMethod"].ToString();
                m.PaymentOrRefundAmount = Convert.ToDouble(r["PaymentOrRefundAmount"].ToString());
                m.PayPalTransactionID = r["PayPalTransactionID"].ToString();
                m.Platform = r["Platform"].ToString();
                m.PositiveFeedbackPercent = Convert.ToDouble(r["PositiveFeedbackPercent"].ToString());
                m.PostageAndPackaging = 0d;
                m.PostageService = r["PostageService"].ToString();
                m.PrivateNotes = r["PrivateNotes"].ToString();
                m.ProductIDType = r["ProductIDType"].ToString();
                m.ProductIDValue = r["ProductIDValue"].ToString();
                m.ProductIDValue2 = r["ProductIDValue2"].ToString();
                int qty = int.Parse(r["Quantity"].ToString());
                m.Quantity = qty;
                m.SaleDate = Convert.ToDateTime(r["SaleDate"].ToString());

                m.SalePrice = Convert.ToDouble(r["SalePrice"].ToString());
                m.SalesRecordNumber = r["SalesRecordNumber"].ToString();
                m.SellingStatus = 1;
                m.TotalPrice = m.Quantity * m.SalePrice;

                m.TransactionID = r["TransactionID"].ToString();
                m.UserId = r["UserId"].ToString();
                m.Variation_Name = r["Variation_Name"].ToString();
                m.Variation_Value = r["Variation_Value"].ToString();
                m.VariationSKU = r["VariationSKU"].ToString();
                m.VariationTitle = r["VariationTitle"].ToString();
                m.VariationViewItemURL = r["VariationViewItemURL"].ToString();

                list.Add(m);
            }
        }
        return list ;
    }

}
